package com.dormitory.manage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory.manage.pojo.StudentDormitory;
import com.dormitory.manage.result.Result;
import com.dormitory.manage.service.StudentDormitoryService;
import com.dormitory.manage.vo.DormitoryQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Administrator
 * @version 1.0
 * @description 宿舍控制层
 * @date 2023/4/7 0007 18:58
 */
@RestController
@RequestMapping("dormitory")
@CrossOrigin
@Slf4j
public class DormitoryController {
    @Resource
    private StudentDormitoryService studentDormitoryService;

    //根据学生id获取学生宿舍信息
    @GetMapping("getSDormitory/{studentId}")
    public Result getStudentDormitory(@PathVariable long studentId){
        StudentDormitory studentDormitory = this.studentDormitoryService.getStudentDormitoryById(studentId);
        return Result.ok(studentDormitory);
    }

    /**
    * @description 带条件分页查询学生宿舍列表
    * @param pageNo
     * @param pageSize
     * @param dormitoryQueryVo
    * @returnType com.dormitory.manage.result.Result
    * @author Administrator
    * @date  19:35
    */
    @PostMapping("getSDormitoryList/{pageNo}/{pageSize}")
    public Result getStudentDormitoryList(@PathVariable int pageNo, @PathVariable int pageSize, @RequestBody DormitoryQueryVo dormitoryQueryVo){
        Page<StudentDormitory> page = new Page<>(pageNo, pageSize);
        Page<StudentDormitory> pageList = this.studentDormitoryService.getStudentDormitoryList(page,dormitoryQueryVo);
        return Result.ok(pageList);
    }

    /**
    * @description 新增学生宿舍
    * @param studentDormitory
    * @returnType com.dormitory.manage.result.Result
    * @author Administrator
    * @date  14:48
    */
    @PostMapping("addSDormitory")
    public Result addStudentDormitory(@RequestBody StudentDormitory studentDormitory){
        boolean result = this.studentDormitoryService.addStudentDormitory(studentDormitory);
        if (result){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
    * @description 根据id删除学生宿舍
    * @param dormitoryId
    * @returnType com.dormitory.manage.result.Result
    * @author Administrator
    * @date  17:10
    */
    @DeleteMapping("deleteSDormitory/{dormitoryId}")
    public Result deleteStudentDormitory(@PathVariable Integer dormitoryId){
        boolean result = this.studentDormitoryService.deleteStudentDormitory(dormitoryId);
        if (result){
            return Result.ok();
        }
        return Result.fail();
    }

    @GetMapping("getSDormitorys")
    public Result getSDormitorys(){
        Long dormitorys = this.studentDormitoryService.getBaseMapper().selectCount(null);
        return Result.ok(dormitorys);
    }
}
